﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using OTA.Notification.BO.Terminal;
using OTA.Notification.Entity.Terminal;
using Strive.DataAccess.Base.BO;
using OTA.Notification.BO.Dictionary;
using System.Collections.Generic;
using OTA.Notification.Entity.Dictionary;

namespace OTA.Notification.Web.Terminal
{

    public class TerminalBO : MsgTerminalBO
    {
        protected override void ValidationRules()
        {
            //
        }
    }

    public partial class Terminal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                InitPage();

                if (Request.QueryString["id"] != null)
                {
                    int iID = 0;

                    if (int.TryParse(Request.QueryString["id"], out iID))

                        BindData(iID);
                }
            }
        }

        private void InitPage()
        {

            //类型加载

            using (DictionaryBO bo = new DictionaryBO())
            {

                IList<OTA_DICTIONARY> lstType = bo.getAllDictionary("Send", OTA.Notification.Common.DicType.Message, OTA.Notification.Common.LanguageType.Chinese);

                ddlMsgType.Items.Insert(0, new ListItem("--发送类型--", ""));

                foreach (OTA_DICTIONARY dic in lstType)
                    ddlMsgType.Items.Add(new ListItem(dic.NAME, dic.VALUE.ToString()));
            }

        }

        private void BindData(int ID)
        {

            using (TerminalBO bo = new TerminalBO())
            {

                MsgTerminalConfigBean bean = bo.Load(ID);

                txtAccount.Text = bean.Account;
                txtCommand.Text = bean.Command;
                txtTerminalType.Text = bean.TerminalType;
                ddlMsgType.SelectedValue = bean.MsgTypeCode;
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (txtAccount.Text == string.Empty)
            {
                Response.Write("<script>alert('请填写帐号！');</script>");
                return;
            }

            if (txtCommand.Text == string.Empty)
            {
                Response.Write("<script>alert('请填写命令！');</script>");
                return;
            }

            if (txtTerminalType.Text == string.Empty)
            {
                Response.Write("<script>alert('请选择终端类型！');</script>");
                return;
            }


            MsgTerminalConfigBean bean = new MsgTerminalConfigBean();

            using (TerminalBO bo = new TerminalBO())
            {

                if (Request.QueryString["id"] != null)
                {
                    int iID = 0;

                    if (int.TryParse(Request.QueryString["id"], out iID))
                        bean = bo.Load(iID);
                    else
                        bean.CreateTime = DateTime.Now;
                }

                bean.MsgTypeCode = ddlMsgType.SelectedValue;

                bean.Account = txtAccount.Text;
                bean.Command = txtCommand.Text;

                bean.TerminalType = txtTerminalType.Text;

                bean.LastUpdateTime = DateTime.Now;

                bo.Save(bean);
            }

            Response.Write("<script>window.parent.Close()</script>");
            
        }
    }
}
